package com.yan.common.mq.dirct;


import com.alibaba.fastjson.JSON;
import com.yan.common.mq.RabbitMqConfig;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
public class RabbitMqSender {

    @Autowired
    private AmqpTemplate template;

    public void send(String str) {

        template.convertAndSend("test", "hello,rabbit " + str);


        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());


//        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE, routeKey,
//                content, correlationData);

        template.convertAndSend("test", "hello,rabbit " + str, correlationData);
    }

    public void sendtest3(String str) {
        template.convertAndSend("test3", "hello,rabbit " + str);
    }


    public void sendMessage() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("test", "yan");
        try {
            MessageProperties messageProperties = new MessageProperties();
            messageProperties.setCorrelationIdString(UUID.randomUUID().toString());


            Message message = MessageBuilder.withBody(JSON.toJSONString(map).getBytes("utf-8"))
                    .andProperties(messageProperties).build();

            CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
            template.send("test2", message);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }


}